package top.dfghhj.anhanx.oauth.service.impl;

import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import top.dfghhj.anhanx.oauth.dao.SysRoleDao;
import top.dfghhj.anhanx.oauth.dao.SysUserRoleDao;
import top.dfghhj.anhanx.oauth.entity.DO.SysRole;
import top.dfghhj.anhanx.oauth.entity.DO.SysUserRole;
import top.dfghhj.anhanx.oauth.service.SysRoleService;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class SysRoleServiceImpl implements SysRoleService {

    @Resource
    private SysRoleDao sysRoleDao;

    @Resource
    private SysUserRoleDao sysUserRoleDao;

    @Override
    public List<SysRole> findByUserId(Long userId) {
        List<SysUserRole> sysUserRoleList = sysUserRoleDao.findByUserId(userId);
        if (CollectionUtils.isEmpty(sysUserRoleList)) {
            return null;
        }
        List<SysRole> sysRoleList = new ArrayList<>();
        for (SysUserRole sysUserRole : sysUserRoleList) {
            SysRole sysRole = sysRoleDao.findById(sysUserRole.getRoleId());
            if (sysRole != null) {
                sysRoleList.add(sysRole);
            }
        }
        return sysRoleList;
    }
}
